import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { BusinessError } from '@kit.BasicServicesKit';

/**
 * 多媒体辅助类
 */
export class MediaHelper {
  private static  Instance:MediaHelper|null = null
  private readonly TAG: string = 'MediaHelper';

  static getInstance(){
    if (!MediaHelper.Instance) {
      MediaHelper.Instance = new MediaHelper()
    }
    return MediaHelper.Instance
  }
  /**
   * 选择图片
   */
  public async selectPicture(maxNumber: number = 1): Promise<string[]> {

    const photoSelectOptions = new photoAccessHelper.PhotoSelectOptions();
    photoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE; // 过滤选择媒体文件类型为IMAGE
    photoSelectOptions.maxSelectNumber =maxNumber; // 选择媒体文件的最大数目
    let uris: Array<string> = [];
    const photoViewPicker = new photoAccessHelper.PhotoViewPicker();
    await photoViewPicker.select(photoSelectOptions)
      .then((photoSelectResult: photoAccessHelper.PhotoSelectResult) => {
        uris = photoSelectResult.photoUris;
        console.info('photoViewPicker.select to file succeed and uris are:' + uris);
      }).catch((err: BusinessError) => {
        console.error(`Invoke photoViewPicker.select failed, code is ${err.code}, message is ${err.message}`);
      })
    return uris
  }
}